<!--
 * @Description: 属性项弹窗
 * @Author: 龙春雨
 * @Date: 2023-04-12 11:22:31
-->
<template>
  <el-dialog
    title="属性项"
    :visible.sync="_visible"
    width="1000px"
    append-to-body
  >
    <avue-crud
      v-if="visible"
      ref="crud"
      v-model="form"
      :data="tableData"
      :page.sync="page"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDelete"
    >
    </avue-crud>
  </el-dialog>
</template>

<script>
import crudMixins from '@/mixins/crud';
import { tableOption } from '@/const/crud/admin/classifyAttributeItem';
import { getPetAttributePage } from '@/api/core/attribute';
import {
  addPetAttribute,
  editPetAttribute,
  delPetAttribute,
} from '@/api/core/attribute';
export default {
  name: 'optionDialog',
  mixins: [crudMixins],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    attributeId: {
      // 属性id
      type: String,
    },
    attributeName: {
      // 属性类型
      type: String,
    },
    attributeCode: {
      // 属性code
      type: String,
    },
  },
  data() {
    return {
      tableOption,
      form: {},
    };
  },
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      },
    },
  },

  methods: {
    // 获取列表
    async getList() {
      try {
        this.tableLoading = true;
        const { data } = await getPetAttributePage({
          ...this.axiosPage,
          ...this.searchForm,
          // attributeId: this.attributeId,
          // attributeType: 1,
          attributeCode: this.attributeCode,
        });
        this.tableData = data.data.records;
        this.page.total = data.data.total;
        this.tableLoading = false;
      } catch (error) {
        this.tableLoading = false;
        throw new Error(error);
      }
    },
    // 编辑
    async handleUpdate(row, index, done, loading) {
      const params = row;
      params.parentAttributeCode = this.attributeCode;
      params.attributeType = 1;
      try {
        await editPetAttribute(params);
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 保存
    async handleSave(row, done, loading) {
      const params = row;
      params.parentAttributeCode = this.attributeCode;
      params.attributeType = 1;
      try {
        await addPetAttribute(params);
        done();
        this.getList();
      } catch (error) {
        loading();
        throw new Error(error);
      }
    },
    // 删除
    handleDelete: function (row, index) {
      this.$confirm('是否确认删除该宠物属性项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delPetAttribute(row.id);
        })
        .then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
    },
    beforeOpen(done) {
      // this.form.attributeName = this.attributeName;
      // this.form.attributeCode = this.attributeCode;
      this.form.parentAttributeCode = this.attributeId;
      this.form.attributeType = '1';
      done();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
